package com.adong.tv.data;

import android.content.Context;
import android.content.SharedPreferences;

import com.adong.tv.model.ApiSource;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * API配置管理
 */
public class ApiConfig {
    
    private static final String PREF_NAME = "movie_pref";
    private static final String KEY_CURRENT_API = "current_api";
    private static final String DEFAULT_API = "heimue";
    
    private static List<ApiSource> apiSources;
    private static ApiSource currentApi;
    private static SharedPreferences preferences;
    
    /**
     * 初始化API配置
     * @param context 应用上下文
     */
    public static void init(Context context) {
        preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        loadApiSources(context);
        
        String currentApiName = preferences.getString(KEY_CURRENT_API, DEFAULT_API);
        for (ApiSource api : apiSources) {
            if (api.getName().equals(currentApiName)) {
                currentApi = api;
                break;
            }
        }
        
        // 如果当前API为空，使用默认的
        if (currentApi == null && !apiSources.isEmpty()) {
            currentApi = apiSources.get(0);
        }
    }
    
    /**
     * 从assets加载API源
     * @param context 应用上下文
     */
    private static void loadApiSources(Context context) {
        apiSources = new ArrayList<>();
        
        try {
            // 从assets中读取api.json
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(context.getAssets().open("movie/api.json")));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            reader.close();
            
            // 解析JSON
            Gson gson = new Gson();
            apiSources = gson.fromJson(sb.toString(), new TypeToken<List<ApiSource>>(){}.getType());
        } catch (IOException e) {
            e.printStackTrace();
            
            // 添加默认API源
            apiSources.add(new ApiSource("heimue", "木耳", 
                    "https://json02.heimuer.xyz/api.php/provide/vod/", 
                    "https://hoplayer.com/index.html?url="));
            apiSources.add(new ApiSource("haohua", "豪华", 
                    "https://hhzyapi.com/api.php/provide/vod/from/hhm3u8/at/json/", 
                    "https://hhjiexi.com/play/?url="));
            apiSources.add(new ApiSource("feifan", "非凡", 
                    "http://cj.ffzyapi.com/api.php/provide/vod/from/ffm3u8/at/json/", 
                    "https://svip.ffzyplay.com/?url="));
        }
    }
    
    /**
     * 获取当前API源
     * @return 当前API源
     */
    public static ApiSource getCurrentApi() {
        return currentApi;
    }
    
    /**
     * 获取所有API源
     * @return API源列表
     */
    public static List<ApiSource> getApiSources() {
        return apiSources;
    }
    
    /**
     * 设置当前API源
     * @param api API源
     */
    public static void setCurrentApi(ApiSource api) {
        currentApi = api;
        preferences.edit().putString(KEY_CURRENT_API, api.getName()).apply();
    }
} 